<?php

class Db_Row extends Zend_Db_Table_Row_Abstract
{
	/**
	 * Cache wrap for parent::findParentRow()
	 *
     * Query a parent table to retrieve the single row matching the current row.
     *
     * @param string|Zend_Db_Table_Abstract $parentTable
     * @param string                        OPTIONAL $ruleKey
     * @param Zend_Db_Table_Select          OPTIONAL $select
     * @return Zend_Db_Table_Row_Abstract   Query result from $parentTable
     * @throws Zend_Db_Table_Row_Exception If $parentTable is not a table or is not loadable.
     */
    public function findParentRow($parentTable, $ruleKey = null, Zend_Db_Table_Select $select = null)
    {
		if (!$parentTable instanceof Db_Peer) {
			$parentTable = call_user_func(array($parentTable, 'getInstance'));
		}

		$map = $this->_prepareReference($this->_getTable(), $parentTable, $ruleKey);

		$referenceColumn = $map['columns'][0];
		if (!$referenceColumn) {
			throw new Zend_Db_Table_Row_Exception('Reference column not found');
		}

		$key = $this->__get($referenceColumn);
		$parentColumn = $map['refColumns'][0];
		if ($parentColumn == 'id') {
			$row = $parentTable->getCache($key, '');
		} else {
			$row = $parentTable->getCache($key, $parentColumn);
		}

		if (!$row) {
			$row = parent::findParentRow($parentTable, $ruleKey, $select);
			if ($row) {
				$parentTable->setCache($row, $parentColumn);
			}
		}

		return $row;
    }


    /**
     * Query a dependent table to retrieve rows matching the current row.
     *
     * @param string|Zend_Db_Table_Abstract  $dependentTable
     * @param string                         OPTIONAL $ruleKey
     * @param Zend_Db_Table_Select           OPTIONAL $select
     * @return Zend_Db_Table_Rowset_Abstract Query result from $dependentTable
     * @throws Zend_Db_Table_Row_Exception If $dependentTable is not a table or is not loadable.
     */
    public function findDependentRowset($dependentTable, $ruleKey = null, Zend_Db_Table_Select $select = null)
    {
		if (!$dependentTable instanceof Db_Peer) {
			$dependentTable = call_user_func(array($dependentTable, 'getInstance'));
		}

		if (0) {
			$map = $this->_prepareReference($dependentTable, $this->_getTable(), $ruleKey);
			$dependentColumn = $map['columns'][0];

			$row = $dependentTable->getCache($this->getId(), $dependentColumn);
			if ($row) {
				$config = array(
					'table'    => $row->getTableClass(),
					'data'     => $row->toArray(),
					'rowClass' => $dependentTable->getRowClass(),
					'readOnly' => false,
					'stored'   => true
				);
				$class = $dependentTable->getRowsetClass();
				$rowset = new $class($config);
				$row = $rowset->current();
			}
		}


 		return parent::findDependentRowset($dependentTable, $ruleKey, $select);
    }

    public function isAlreadyBuy(){
    	return false;
    }


    /**
     * Get comments of substance by page number.
     *
     * @param integer $page
     * @return Db_Rowset
     */
    public function getComments()
    {
		$substanceTypeRow = CommentSubstanceTypePeer::fetchBy(CommentSubstanceTypePeer::TABLE, $this->getTable());

		$select = $commentPeer->selectApproved();
		$select->where(CommentPeer::SUBSTANCE_ID, $this->_getId());
		$select->where(CommentPeer::SUBSTANCE_TYPE_ID, $substanceTypeRow->getId());
		$select->limitPage($page, 10);

		// Order by create date
		$select->order(CommentPeer::CREATED . Db_Select::SQL_DESC);

		// Filter by parent_id
		$select->where(CommentPeer::PARENT_ID, $parentId);

		$rowset = $commentPeer->fetchAll($select);

		$comments = array();
		foreach ($rowset as $commentRow) {
			$comment = $commentRow->toArray();
			// Adding profile row with user information
			$profileRow = $commentRow->getProfileRowByProfileId();
			$comment['profile']['name'] = $profileRow->getName();
			$comment['profile']['url_img'] = $profileRow->getAvatar();
			$comment['profile']['url'] = u(array('controller' => 'community-people', 'action' => 'about', 'id' => $profileRow['id']), null, true);
			// Adding date and time for comment
			$time = strtotime($commentRow->getCreated());

			// Adding nesting level
			$comment['level'] = $level;

			// Set showDelete for show delete link
			$showDelete = Debug::getInstance()->isDebugMode();
			if (!$showDelete && $currentUser = $this->_getUser()) {
				$showDelete = ($profileRow->getId() == $currentUser->getId());
			}
			$comment['showDelete'] = $showDelete;

			$level += 1;
			$comment['tree'] = $this->_initComments($substanceId, $substanceTypeId, $commentRow->getId(), $level);
			$comments[] = $comment;
		}

		if ($parentId) {
			return $comments;
		} else {
			$this->view->comments = $comments;
		}
	}
	
	public function save()
	{
		$modifiedFields = $this->_modifiedFields;
		$data = $this->_data;
		foreach ($modifiedFields as $col => $val) {
			$data[$col] = str_ireplace('<', '&lt;', $data[$col]);
			$data[$col] = str_ireplace('>', '&gt;', $data[$col]);
		}
		$this->_data = $data;
		parent::save();
	}

}